package com.firebird.app.kotlin.stock.trend.activity

import android.os.Bundle
import android.view.View
import android.widget.AdapterView.OnItemClickListener
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import com.firebird.app.kotlin.stock.trend.adapter.StockRankDataAdapter
import com.firebird.app.kotlin.stock.trend.bean.Stock
import com.firebird.app.kotlin.stock.trend.service.StockService
import com.firebird.app.kotlin.stock.trend.service.UIListener
import com.firebird.app.kotlin.stock.trend.view.FooterMenuView
import com.firebird.app.kotlin.stock.trend.view.HVListView
import com.firebird.app.kotlin.stock.trend.view.SimpleCommonTitleView
import com.firebird.app.kotlin.stock.trend.R
import com.firebird.app.kotlin.stock.trend.utils.Constant
import com.firebird.app.kotlin.stock.trend.utils.DensityUtil.dip2px

/**
 * 显示沪深A股大盘实时数据的窗口界面
 * @author ljk
 * @since  2019年4月18日
 */
class HSAStockActivity : AppCompatActivity(), View.OnClickListener, UIListener {
    //通用窗口title
    private var simpleTitle: SimpleCommonTitleView? = null
    private var menu: FooterMenuView? = null
    private var hvlistView: HVListView? = null
    private var adapter: StockRankDataAdapter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.setContentView(R.layout.activity_hsastock)
        //请求远程服务器上的股票排序数据
        StockService.getQuotationRank(0, 10, Constant.STOCK_COLUMN_NOWPRICE, Constant.ORDER_BY_DESC, 1001, this)
        //初始化titlebar
        simpleTitle = findViewById<View>(R.id.simple_title) as SimpleCommonTitleView
        simpleTitle!!.setOnLeftClickListener(this)
        simpleTitle!!.setOnRightClickListener(this)
        //初始化底部菜单控件
        menu = findViewById<View>(R.id.footer_menu) as FooterMenuView
        menu!!.setCurrentMenu(R.id.menu_quotation)
        menu!!.setOnTouchListener(menu!!.MenuOnTouchListener())
        hvlistView = findViewById<View>(R.id.hv_list) as HVListView
        //设置可滚动的列头
        hvlistView!!.setListHead(findViewById<View>(R.id.scrollColumn) as LinearLayout)
        //设置可滚动空间的宽度
        hvlistView!!.setScrollWidth(dip2px(this, 630 + 70 + 10.toFloat()))
        adapter = StockRankDataAdapter(this, hvlistView!!)
        hvlistView!!.onItemClickListener = OnItemClickListener { parent, view, position, id ->
            //Intent intent = new Intent(HSAStockActivity.this,StockDetailActivity.class);
//startActivity(intent);
        }
    }

    override fun onStart() {
        super.onStart()
        //设置数据
        hvlistView!!.adapter = adapter
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.title_right_but -> toSearch()
            R.id.title_left_but -> finish()
        }
    }

    /**
     * 进入股票代码搜索页面
     */
    private fun toSearch() {}

    override fun refresh(vararg params: Any?) {
        val stockList = params[0] as List<Stock>
        for (s in stockList) {
            println(s.name)
        }
        adapter!!.setData(stockList)
        adapter!!.notifyDataSetInvalidated()
    }
}